package com.cqjtu.eecs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqjtu.eecs.entity.Major;
import com.cqjtu.eecs.entity.TrainingProject;
import com.cqjtu.eecs.entity.dto.MajorBriefDTO;
import com.cqjtu.eecs.exception.NotFoundException;
import com.cqjtu.eecs.mapper.MajorMapper;
import com.cqjtu.eecs.mapper.TrainingProjectMapper;
import com.cqjtu.eecs.service.ITrainingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqjtu.eecs.utils.Assert;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author   luojun
 * @since 2022-02-28
 */
@Service
public class TrainingProjectServiceImpl extends ServiceImpl<TrainingProjectMapper, TrainingProject> implements ITrainingProjectService {
    private final TrainingProjectMapper trainingProjectMapper;
    private final MajorMapper majorMapper;

    public TrainingProjectServiceImpl(TrainingProjectMapper trainingProjectMapper, MajorMapper majorMapper) {
        this.trainingProjectMapper = trainingProjectMapper;
        this.majorMapper = majorMapper;
    }

    /**
     * 通过专业代码获取专业培养方案模板一
     * @return MajorBriefDTO 专业简介数据传输模型
     */
    @Override
    public MajorBriefDTO getTrainingProjectDTO(String id) {
        // 获取专业信息
        Major major = majorMapper.selectById(id);
        Assert.notNull(major, new NotFoundException("不存在该专业"));
        // 获取专业下的培养方案模板
        TrainingProject trainingProject = trainingProjectMapper.selectOne(new LambdaQueryWrapper<TrainingProject>()
                .eq(TrainingProject::getMajorCode, id));
        Assert.notNull(trainingProject, new NotFoundException("该专业未添加模板"));
        // 封装为DTO
        return new MajorBriefDTO(major, trainingProject);
    }

    /**
     * 判断专业下是否存在模板
     * @param majorCode 专业代码
     * @return
     */
    @Override
    public boolean hasTemplate(String majorCode) {
        TrainingProject trainingProject = trainingProjectMapper.selectOne(new LambdaQueryWrapper<TrainingProject>()
                .eq(TrainingProject::getMajorCode, majorCode));
        if (trainingProject == null)
            return false;
        return true;
    }
}
